# -*- coding: utf-8 -*-

import struct
import re
import os
import math
from num_trans_func import *
from misc_func import *
from tkinter import *
from tkinter import ttk

def tk_main():
	global mode
	mode = 0
	
	def choose(event): #选择框事件
		global mode
		mode = com.current()
	pass

	def start_trans(): #确认键
		int_w = txt_int_w.get(1.0, "end").strip()      #整数位宽
		dec_w = txt_dec_w.get(1.0, "end").strip()      #小数位宽
		value = txt_in.get(1.0, "end").strip()         #输入数值
		shift_n = 2 ** int(dec_w)                      #需要除以的值
		max_f = (2 ** (int(int_w)+int(dec_w)))/shift_n #能表示的数值上限
		whole_w = int(int_w)+int(dec_w)				   #总位宽
		
		if mode == 0: #十进制
			dec = value
			dec_shift = int(float(dec) * shift_n)
			if float(dec) >= max_f:
				bin = "超出范围"
				bnr = "超出范围"
			else:
				bin = dec_to_bin(str(dec_shift))
				bnr = dec_to_bnr(str(dec_shift))
		elif mode == 1: #二进制
			dec = bin_to_dec(str(value))
			dec = float(dec)/shift_n
			bin = value
			bnr = bin_to_bnr(value)
		else: #补码二进制
			dec = bnr_to_dec(str(value))
			dec = float(dec)/shift_n
			bin = bnr_to_bin(value)
			bnr = value
		
		bin_hex = "NONE"
		bnr_hex = "NONE"

		if float(dec) < max_f:
			bin_hex = "'h" + bin_to_hex(bin)
			bnr_hex = "'h" + bin_to_hex(bnr)
		
		txt_dec.delete(0.0, END)
		txt_dec.insert(1.0, dec)
		txt_bin.delete(0.0, END)
		txt_bin.insert(1.0, bin)
		txt_bnr.delete(0.0, END)
		txt_bnr.insert(1.0, bnr)
		txt_bin_hex.delete(0.0, END)
		txt_bin_hex.insert(1.0, bin_hex)
		txt_bnr_hex.delete(0.0, END)
		txt_bnr_hex.insert(1.0, bnr_hex)
		
	pass
	
	root = Tk()
	root.geometry("400x195")
	root.title("定点数转换器 from 尼德兰的喵")
	
	#选择输入的进制和数值
	f1 = Frame(root, height = 100, width = 300)
	f1.pack(pady=1)
	#创建下拉菜单
	xVariable = StringVar()
	com = ttk.Combobox(f1, textvariable=xVariable, cursor="arrow")
	com["value"] = ("十进制", "二进制", "二进制补码")
	com.current(0)
	com.bind("<<ComboboxSelected>>", choose)
	com.pack(side = LEFT)
	#数值框
	txt_in = Text(f1, height=1)
	txt_in.pack(side = RIGHT, padx=10)

	#位宽配置
	f_wd_cfg = Frame(root, height = 100, width = 300, relief=RIDGE)
	f_wd_cfg.pack(fill=X, pady=1)
	label_int_w = Label(f_wd_cfg,text='整数位宽').pack(side=LEFT)
	txt_int_w = Text(f_wd_cfg,width = 18, height = 1)
	txt_int_w.pack(side=LEFT, padx=10)
	txt_int_w.delete(0.0, END)
	txt_int_w.insert(1.0, "8")
	
	txt_dec_w = Text(f_wd_cfg,width = 18, height = 1)
	txt_dec_w.pack(side=RIGHT, padx=10)
	label_dec_w = Label(f_wd_cfg,text='小数位宽').pack(side=RIGHT)
	txt_dec_w.delete(0.0, END)
	txt_dec_w.insert(1.0, "8")
	
	#分割线
	separator = ttk.Separator(root, orient='horizontal')
	separator.pack(fill='x', padx=10, pady=10)

	f_dec = Frame(root, height = 100, width = 300, relief=RIDGE)
	f_dec.pack(fill=X, pady=1)
	labe_dec = Label(f_dec,text='十进制').pack(side=LEFT)
	txt_dec = Text(f_dec,width = 30, height = 1)
	txt_dec.pack(side=RIGHT, padx=10)

	f_bin = Frame(root, height = 100, width = 300, relief=RIDGE)
	f_bin.pack(fill=X, pady=1)
	labe_bin = Label(f_bin,text='二进制').pack(side=LEFT)
	txt_bin = Text(f_bin,width = 30, height = 1)
	txt_bin.pack(side=RIGHT, padx=10)
	txt_bin_hex = Text(f_bin,width = 10, height = 1)
	txt_bin_hex.pack(side=RIGHT, padx=10)

	f_bnr = Frame(root, height = 100, width = 300, relief=RIDGE)
	f_bnr.pack(fill=X, pady=1)
	labe_bnr = Label(f_bnr,text='二进制补码').pack(side=LEFT)
	txt_bnr = Text(f_bnr,width = 30, height = 1)
	txt_bnr.pack(side=RIGHT, padx=10)
	txt_bnr_hex = Text(f_bnr,width = 10, height = 1)
	txt_bnr_hex.pack(side=RIGHT, padx=10)

	fx = Frame(root, height = 100, width = 300)
	fx.pack(pady=10)
	button1 = Button(fx, text='确定', command=start_trans)
	button1.pack(side=LEFT, padx=10)
	button2 = Button(fx, text='退出', command=root.quit)
	button2.pack(side=RIGHT, padx=10)
	
	root.mainloop()

if __name__ == '__main__':
	tk_main()
